<?php
/**
 * Created by PhpStorm.
 * User: FoxxxZhu
 * Date: 2018/3/20
 * Time: 14:38
 */

namespace Forxx;

use Forxx\Api\Utils;
use Lavender\Core;
use Lavender\Errno;
use Lavender\Exception;

class ForxxWebApiService extends ForxxWebService
{
    protected $without_auth_actions = ['*'];
    protected $without_sign_actions = ['*'];

    public function before_execute()
    {
        if(!$this->check_ip()){
            $ip = Utils::get_ip(true);
            throw new Exception\Auth('ip ['.$ip.'] disallow', Errno::AUTH_FAILED);
        }
        return parent::before_execute();
    }

    public function check_ip(){
        $ip = Utils::get_ip(true);
        $allow_ips = $this->get_allow_ips();   
        if(empty($allow_ips)){
            return false;
        }
        if(array_search('*',$allow_ips) !== false || array_search($ip,$allow_ips) !== false){
            return true;
        }
        else{
            return false;
        }
    }

    public function get_allow_ips(){
        $ips =  Core::get_config("const","IP_white_list"); 
        $name = L_APP_NAME; 
        if(isset($ips[$name])){
            return $ips[$name];
        }
        return null;
    }
}